<?php
/* 公共方法类 */
namespace Shop\Controller;
use Think\Controller;
use User\Api\UserApi;

class PublicController extends Controller{
	/**
     * 商户后台用户登录
     */
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){
            // /* 检测验证码 TODO: */
            // if(!check_verify($verify)){
            //     $this->error('验证码输入错误！');
            // }
            $User   =   new UserApi;
            $uid    =   $User->login($username, $password, 1, 2);
            if($uid > 0){
                /* 记录登录SESSION和COOKIES */
                $auth = array(
                    'uid'             => $uid,
                    'username'        => $username,
                );
                session('shop_auth', $auth);
                successTip('登录成功！', U('Index/index'));
            }else{
                $msg = $uid == -1 ? "用户不存在或者未审核通过 ":"密码错误";
                errorTip($msg);
            }
        } else {
            $user = session('shop_auth');
            if(!empty($user['uid'])){
                $this->redirect('Index/index');
            }else{
                $this->assign("pagetitle","商家登录");
                $this->display();
            }
        }
    }
    public function changepwd(){
        $UC_AUTH_KEY = '5dV~j_E,GPF4fx^.;%Lol9yNU-W<(#>HkS}=gT|]';
        if(IS_POST){
            $user = session("shop_auth");
            $pwd = $_POST['oldpwd'];
            $npwd = $_POST['newpwd'];
            $rs = M("ucenter_member")->where(array('id'=>$user['uid']))->find();
            $pwd = md5(sha1($pwd) . $UC_AUTH_KEY); //echo $user['uid'].'__'.$pwd.'__'.$rs['password'];exit;
            if($pwd !== $rs['password']){
                errorTip("密码输入错误");
            }
            $npwd = md5(sha1($npwd) . $UC_AUTH_KEY);
            $r = M("ucenter_member")->where(array('id'=>$user['uid']))->save(array('password'=>$npwd));
            if($r !== false){
                successTip("修改成功!");
            }else{
                errorTip("修改失败,".$r['info']);
            }
        }else{  
            $this->assign("pagetitle","修改密码");  
            $this->display();
        }
    }
    public function loginout(){
        session('shop_auth', null);
        session("shop_common_info", null);
        session_destroy();
        successTip('退出成功！', U('Public/login'));
    }
    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(5);
    }

}